package com.febers.ihealth

import android.widget.CompoundButton
import androidx.appcompat.widget.Toolbar
import com.febers.ihealth.base.BaseActivity
import com.febers.ihealth.entity.SurveyFinishEvent
import com.febers.ihealth.utils.*
import kotlinx.android.synthetic.main.activity_before_survey.*
import kotlinx.android.synthetic.main.toolbar_common.*

class BeforeSurveyAct: BaseActivity(), CompoundButton.OnCheckedChangeListener, EventListener {

    private var checkedCount = 0

    override fun afterCreate() {
        user.clearBeforeSurvey()
        check_box_fare.setOnCheckedChangeListener(this)
        check_box_fumai.setOnCheckedChangeListener(this)
        check_box_penti_bisai_liuti.setOnCheckedChangeListener(this)
        check_box_ehan_efeng.setOnCheckedChangeListener(this)
        check_box_fare_toutong_jiroutengtong_gujeitengtong.setOnCheckedChangeListener(this)
        check_box_kesou_qichuan.setOnCheckedChangeListener(this)
        check_box_yangan_yantong_yanyang.setOnCheckedChangeListener(this)
        btn_survey_0_next.setOnClickListener {
            if (checkedCount > 0) {
                nav(SurveyContainer::class.java)
            } else {
                toast("请至少选择一个症状")
            }
        }
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        buttonView ?: return
        when(buttonView.id) {
            R.id.check_box_fare -> {
                user.fare = isChecked
            }
            R.id.check_box_fumai -> {
                user.fumai = isChecked
            }
            R.id.check_box_penti_bisai_liuti -> {
                user.pentiBisaiLiuti = isChecked
            }
            R.id.check_box_ehan_efeng -> {
                user.ehanEfeng = isChecked
            }
            R.id.check_box_fare_toutong_jiroutengtong_gujeitengtong -> {
                user.toutongJiroutengtongGujietengtong = isChecked
            }
            R.id.check_box_kesou_qichuan -> {
                user.kesouQichuan = isChecked
            }
            R.id.check_box_yangan_yantong_yanyang -> {
                user.yanganYantongYanyang = isChecked
            }
        }
        if (isChecked) {
            checkedCount++
        } else {
            checkedCount--
        }
    }

    override fun setView(): Int = R.layout.activity_before_survey

    override fun setToolbar(): Toolbar? = toolbar_common

    override fun setToolbarTitle(): String? = "主要症状"

    override fun onEvent(event: Any, fromParents: Boolean) {
        if (event is SurveyFinishEvent) {
            if (event.finish) {
                finish()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        Event.register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        Event.unregister(this)
    }
}